package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDeviceMapper;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 物资信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-20
 */
@Service
public class SysDeviceServiceImpl implements ISysDeviceService {

    private static final Logger log = LoggerFactory.getLogger(SysDeviceServiceImpl.class);

    @Autowired
    private SysDeviceMapper sysDeviceMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询物资信息
     * 
     * @param deviceId 物资信息主键
     * @return 物资信息
     */
    @Override
    public SysDevice selectSysDeviceByDeviceId(Long deviceId)
    {
        return sysDeviceMapper.selectSysDeviceByDeviceId(deviceId);
    }

    /**
     * 查询物资信息列表
     * 
     * @param sysDevice 物资信息
     * @return 物资信息
     */
    @Override
    public List<SysDevice> selectSysDeviceList(SysDevice sysDevice)
    {
        return sysDeviceMapper.selectSysDeviceList(sysDevice);
    }

    /**
     * 新增物资信息
     * 
     * @param sysDevice 物资信息
     * @return 结果
     */
    @Override
    public int insertSysDevice(SysDevice sysDevice)
    {
        sysDevice.setCreateTime(DateUtils.getNowDate());
        return sysDeviceMapper.insertSysDevice(sysDevice);
    }

    /**
     * 修改物资信息
     * 
     * @param sysDevice 物资信息
     * @return 结果
     */
    @Override
    public int updateSysDevice(SysDevice sysDevice)
    {
        sysDevice.setUpdateTime(DateUtils.getNowDate());
        return sysDeviceMapper.updateSysDevice(sysDevice);
    }

    /**
     * 批量删除物资信息
     * 
     * @param deviceIds 需要删除的物资信息主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceByDeviceIds(String deviceIds)
    {
        return sysDeviceMapper.deleteSysDeviceByDeviceIds(Convert.toStrArray(deviceIds));
    }

    /**
     * 删除物资信息信息
     * 
     * @param deviceId 物资信息主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceByDeviceId(Long deviceId)
    {
        return sysDeviceMapper.deleteSysDeviceByDeviceId(deviceId);
    }

    @Override
    public String importDevice(List<SysDevice> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysDevice obj : list)
        {
            try
            {
                // 验证是否存在这个公司
                SysDevice c = sysDeviceMapper.selectSysDeviceByDeviceId(obj.getDeviceId());
                if (StringUtils.isNull(c))
                {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setCreateBy(operName);
                    this.insertSysDevice(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、物资 " +obj.getDeviceName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setUpdateBy(operName);
                    this.updateSysDevice(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、物资 " +obj.getDeviceName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、物资 " +obj.getDeviceName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、物资 " +obj.getDeviceName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
